package org.example;

import java.util.ArrayList;
import java.util.List;


public class ComputerMode {
    private Foods foods=new Foods(25,30,30);
    ArrayList<Snake> computerSnakes= new ArrayList<>();

    // 寻找最近的食物
    public Point findNearestFood(Snake snake) {
        Point head = snake.getHead();
        List<Point> foodList = foods.getFoods();
        Point nearestFood = null;
        int minDistance = Integer.MAX_VALUE;

        for (Point food : foodList) {
            int distance = Math.abs(head.getGridX() - food.getGridX()) + Math.abs(head.getGridY() - food.getGridY());
            if (distance < minDistance) {
                minDistance = distance;
                nearestFood = food;
            }
        }
        return nearestFood;
    }

    // 电脑蛇向食物移动
    public void moveTowardsFood(Snake snake, Point food) {
        Point head = snake.getHead();
        int dx = food.getGridX() - head.getGridX();
        int dy = food.getGridY() - head.getGridY();

        if (Math.abs(dx) > Math.abs(dy)) {
            if (dx > 0) {
                snake.setDirection(SnakeGame.Direction.RIGHT);
            } else {
                snake.setDirection(SnakeGame.Direction.LEFT);
            }
        } else {
            if (dy > 0) {
                snake.setDirection(SnakeGame.Direction.DOWN);
            } else {
                snake.setDirection(SnakeGame.Direction.UP);
            }
        }
    }
}
